package in.muton.edushare.newentities;

import in.muton.edushare.EduShareSession;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.UserRolesDAO;
import com.edushare.common.dao.UsersDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class UnivUserCreator extends UserInviter {
	private static final long serialVersionUID = 747803096472728383L;
	private IModel<Universities> univ;
	private ORMInterface ormInterface;
	private Integer role;

	public UnivUserCreator(ORMInterface ormInterface,
			IModel<Universities> univ, Integer role) {
		super(ormInterface);
		this.univ = univ;
		this.role = role;
		this.ormInterface = ormInterface;
	}

	public List<String> addUsersToUniv(String userList) {
		String[] list = UserInviter.breakString(userList);
		List<String> statusList = new ArrayList<String>();
		UserRolesDAO addDAO = new UserRolesDAO(ormInterface);
		for (String s : list) {
			Users userVO = this.getUserByString(s);
			if (userVO != null) {
				statusList.add(addDAO.addUserFB(univ.getObject(), userVO, role, 
						EduShareSession.get().getLoggedInUser()));
			} else {
				if (isEmailId(s)) {
					UsersDAO uDAO = new UsersDAO(ormInterface);
					userVO = uDAO.newUser(s.trim());
					statusList.add(addDAO.addUserFB(univ.getObject(), userVO, role,
						EduShareSession.get().getLoggedInUser()));
				} else {
					statusList.add("Unable to determine the type of user " + s);
				}
			}
		}
		return statusList;
	}
}
